Imports DotNetNuke.Entities.Profile

Imports Microsoft.VisualBasic

Namespace effority.UserDirectory.Scheduler

   Public Class FakeView

#Region " Private Members "

      Private _PortalId As Int32
      'Private _PortalSettings As DotNetNuke.Entities.Portals.PortalSettings

      Private _ModuleId As Int32
      Private _TabModuleId As Int32

      Private _Settings As Generic.Dictionary(Of String, String)

      Private _ProfilesPropertyCollectionToShow As Generic.List(Of DotNetNuke.Entities.Profile.ProfilePropertyDefinition)

      Private _ProfilesPropertyCollectionToSearch As Generic.List(Of DotNetNuke.Entities.Profile.ProfilePropertyDefinition)

      Private _ProfilesPropertyCollectionToLoad As Generic.List(Of DotNetNuke.Entities.Profile.ProfilePropertyDefinition)

      Private _ProfilePropertyCollection As DotNetNuke.Entities.Profile.ProfilePropertyDefinitionCollection

      Private _EnabledRoles As Generic.List(Of Integer)

      Private _RolesConfiguration As Generic.Dictionary(Of Int32, Enums.RoleStatus)

#End Region

#Region " Properties "

      Public Property PortalId() As Int32
         Get
            Return _PortalId
         End Get
         Set(ByVal value As Int32)
            _PortalId = value
         End Set
      End Property

      Public Property ModuleId() As Int32
         Get
            Return _ModuleId
         End Get
         Set(ByVal value As Int32)
            _ModuleId = value
         End Set
      End Property

      Public Property TabModuleId() As Int32
         Get
            Return _TabModuleId
         End Get
         Set(ByVal value As Int32)
            _TabModuleId = value
         End Set
      End Property

      Private ReadOnly Property Settings() As Generic.Dictionary(Of String, String)
         Get

            'If _Settings Is Nothing Then

            Dim modSettings As Hashtable = DotNetNuke.Entities.Portals.PortalSettings.GetModuleSettings(Me.ModuleId)
            Dim tabModSettings As Hashtable = DotNetNuke.Entities.Portals.PortalSettings.GetTabModuleSettings(Me.TabModuleId)

            _Settings = New Generic.Dictionary(Of String, String)

                For Each pair In modSettings

                    _Settings.Add(Convert.ToString(pair.Key), Convert.ToString(pair.Value))

                Next

                For Each pair In tabModSettings

                    If Not _Settings.TryGetValue(Convert.ToString(pair.Key), Nothing) Then
                        _Settings.Add(Convert.ToString(pair.Key), Convert.ToString(pair.Value))
                    End If

                Next

            'End If

            Return _Settings

         End Get

      End Property

      Private ReadOnly Property UserTableCacheKey() As String
         Get
            Return Consts.MODULE_BASE & "." & ModuleId
         End Get
      End Property

      Private ReadOnly Property USERTABLE_MODULE_CACHE() As String
         Get
            Return Consts.USERTABLE_CACHE & Me.ModuleId
         End Get
      End Property

      Private ReadOnly Property SESSION_KEY() As String
         Get

            Return Consts.MODULE_BASE & Me.ModuleId

         End Get
      End Property

#End Region

      Public Sub makeCache()
         GetUserTable()
      End Sub

      Private Function GetUserTable() As DataView

         Dim UserTable As DataTable


         UserTable = New System.Data.DataTable

         UserTable.BeginLoadData()

         Dim profilePropertyCollection As DotNetNuke.Entities.Profile.ProfilePropertyDefinitionCollection = _
                    DotNetNuke.Entities.Profile.ProfileController.GetPropertyDefinitionsByPortal(PortalId)

         ' load users 
         Dim UserListe As Generic.List(Of effority.UserDirectory.UserIdAndUserName)

         UserListe = Me.GetUsersList()

         ' load profile data 

         Dim userProfileDic As Generic.Dictionary(Of Integer, Generic.Dictionary(Of Integer, String))

         userProfileDic = effority.UserDirectory.Controller.GetUserProfileDic(Me.ProfilesPropertiesToLoad, PortalId)

         UserTable.Columns.Add(effority.UserDirectory.Consts.USERID_COLUMN, GetType(Integer))

         ' build dataTable
         For Each info As DotNetNuke.Entities.Profile.ProfilePropertyDefinition In Me.ProfilesPropertiesToLoad

            UserTable.Columns.Add(Me.CleanColumnName(info.PropertyName))

         Next

         For Each user As effority.UserDirectory.UserIdAndUserName In UserListe

            Dim workrow As System.Data.DataRow = UserTable.NewRow()

            Dim profileForThisUser As Generic.Dictionary(Of Integer, String)

            workrow(effority.UserDirectory.Consts.USERID_COLUMN) = user.UserId

            If userProfileDic.TryGetValue(user.UserId, profileForThisUser) Then

               For Each info As DotNetNuke.Entities.Profile.ProfilePropertyDefinition In Me.ProfilesPropertiesToLoad

                  If info.PropertyDefinitionId = -1 Then
                     workrow(info.PropertyName) = user.UserName
                  ElseIf info.PropertyDefinitionId = -2 Then
                     workrow(info.PropertyName) = user.Email
                  Else
                     If profileForThisUser.TryGetValue(info.PropertyDefinitionId, Nothing) Then
                        workrow(CleanColumnName(info.PropertyName)) = profileForThisUser(info.PropertyDefinitionId)
                     Else
                        workrow(CleanColumnName(info.PropertyName)) = ""
                     End If
                  End If

               Next

            End If

            UserTable.Rows.Add(workrow)

         Next

         UserTable.EndLoadData()

         DotNetNuke.Services.Cache.CachingProvider.Instance.Insert(Me.USERTABLE_MODULE_CACHE, UserTable, False)

         Dim dv As DataView = UserTable.DefaultView

         Return dv

      End Function

      Private Function GetUsersList() As Generic.List(Of effority.UserDirectory.UserIdAndUserName)

         Dim liste As Generic.List(Of effority.UserDirectory.UserIdAndUserName)

         ' doof

         liste = effority.UserDirectory.Controller.GetUsersInRoles(Me.RolesConfiguration, False)

         Return liste

      End Function

      Private ReadOnly Property ProfilesPropertyCollectionToShow() As Generic.List(Of DotNetNuke.Entities.Profile.ProfilePropertyDefinition)

         Get

            If _ProfilesPropertyCollectionToShow Is Nothing Then

               _ProfilesPropertyCollectionToShow = New Generic.List(Of DotNetNuke.Entities.Profile.ProfilePropertyDefinition)

               If Me.ShowAllProfiles Then

                  For Each info As DotNetNuke.Entities.Profile.ProfilePropertyDefinition In Me.ProfilePropertyCollection

                     _ProfilesPropertyCollectionToShow.Add(info)

                  Next

               Else

                  If Not Settings(Consts.PROFILES_TO_SHOW) Is Nothing Then

                     Dim str As String = Settings(Consts.PROFILES_TO_SHOW).ToString()

                     str = str.Replace(";;", ";")

                     Dim arr As String() = str.Split(";"c)

                     For Each id As String In arr

                        If Not String.IsNullOrEmpty(id) Then

                           If CInt(id) = -1 Then

                              _ProfilesPropertyCollectionToShow.Insert(0, Controller.FakeUsernameProfileProperty)

                           ElseIf CInt(id) = -2 Then

                              _ProfilesPropertyCollectionToShow.Insert(0, Controller.FakeEmailProfileProperty)

                           Else

                              Dim pd As DotNetNuke.Entities.Profile.ProfilePropertyDefinition = ProfilePropertyCollection.GetById(CInt(id))

                              If Not pd Is Nothing Then

                                 _ProfilesPropertyCollectionToShow.Add(pd)

                              End If

                           End If

                        End If

                     Next

                  End If

               End If

            End If


            Return _ProfilesPropertyCollectionToShow

         End Get

      End Property

      Private ReadOnly Property ProfilesPropertyCollectionToSearch() As Generic.List(Of DotNetNuke.Entities.Profile.ProfilePropertyDefinition)
         Get

            If _ProfilesPropertyCollectionToSearch Is Nothing Then

               _ProfilesPropertyCollectionToSearch = New Generic.List(Of DotNetNuke.Entities.Profile.ProfilePropertyDefinition)

               If Me.SearchAllProfiles Then

                  For Each info As DotNetNuke.Entities.Profile.ProfilePropertyDefinition In Me.ProfilePropertyCollection

                     _ProfilesPropertyCollectionToSearch.Add(info)

                  Next

               Else

                  If Not Settings(Consts.PROFILES_TO_SEARCH) Is Nothing Then

                     Dim str As String = Settings(Consts.PROFILES_TO_SEARCH).ToString()

                     str = str.Replace(";;", ";")

                     Dim arr As String() = str.Split(";"c)

                     For Each id As String In arr

                        If Not String.IsNullOrEmpty(id) Then

                           If CInt(id) = -1 Then

                              _ProfilesPropertyCollectionToSearch.Insert(0, Controller.FakeUsernameProfileProperty)

                           ElseIf CInt(id) = -2 Then

                              _ProfilesPropertyCollectionToSearch.Insert(0, Controller.FakeEmailProfileProperty)

                           Else

                              Dim pd As DotNetNuke.Entities.Profile.ProfilePropertyDefinition = ProfilePropertyCollection.GetById(CInt(id))

                              If Not pd Is Nothing Then

                                 _ProfilesPropertyCollectionToSearch.Add(pd)

                              End If

                           End If

                        End If

                     Next

                  End If

               End If

            End If


            Return _ProfilesPropertyCollectionToSearch

         End Get

      End Property

      Private ReadOnly Property ProfilePropertyCollection() As ProfilePropertyDefinitionCollection

         Get

            If _ProfilePropertyCollection Is Nothing Then

               _ProfilePropertyCollection = ProfileController.GetPropertyDefinitionsByPortal(PortalId)

               _ProfilePropertyCollection.Insert(0, Controller.FakeUsernameProfileProperty())

               _ProfilePropertyCollection.Insert(1, Controller.FakeEmailProfileProperty)

            End If

            Return _ProfilePropertyCollection

         End Get

      End Property

      Private ReadOnly Property ProfilesPropertiesToLoad() As Generic.List(Of DotNetNuke.Entities.Profile.ProfilePropertyDefinition)
         Get

            If _ProfilesPropertyCollectionToLoad Is Nothing Then

               _ProfilesPropertyCollectionToLoad = New Generic.List(Of DotNetNuke.Entities.Profile.ProfilePropertyDefinition)

               If Me.SearchAllProfiles = True Or Me.ShowAllProfiles = True Then

                  For Each info As DotNetNuke.Entities.Profile.ProfilePropertyDefinition In Me.ProfilePropertyCollection

                     _ProfilesPropertyCollectionToLoad.Add(info)

                  Next

               Else

                  Dim dic As New Generic.Dictionary(Of Int32, DotNetNuke.Entities.Profile.ProfilePropertyDefinition)

                  For Each info As DotNetNuke.Entities.Profile.ProfilePropertyDefinition In Me.ProfilesPropertyCollectionToSearch

                     dic.Add(info.PropertyDefinitionId, info)

                  Next

                  For Each info As DotNetNuke.Entities.Profile.ProfilePropertyDefinition In Me.ProfilesPropertyCollectionToShow

                     If Not dic.TryGetValue(info.PropertyDefinitionId, Nothing) Then

                        dic.Add(info.PropertyDefinitionId, info)

                     End If

                  Next

                  For Each info As DotNetNuke.Entities.Profile.ProfilePropertyDefinition In dic.Values

                     _ProfilesPropertyCollectionToLoad.Add(info)

                  Next

               End If

            End If

            Return _ProfilesPropertyCollectionToLoad

         End Get

      End Property

      Public ReadOnly Property RolesConfiguration() As Generic.Dictionary(Of Int32, Enums.RoleStatus)
         Get

            If _RolesConfiguration Is Nothing Then

               Dim addRoles As String() = CStr(Settings(Consts.ADD_ROLES)).Split(";"c)
               Dim removeRoles As String() = CStr(Settings(Consts.REMOVE_ROLES)).Split(";"c)
               Dim ignoreRoles As String() = CStr(Settings(Consts.IGNORE_ROLES)).Split(";"c)

               For Each str As String In addRoles
                  If Not String.IsNullOrEmpty(str) Then
                     _RolesConfiguration.Add(CInt(str), Enums.RoleStatus.Add)
                  End If
               Next

               For Each str As String In removeRoles
                  If Not String.IsNullOrEmpty(str) Then
                     _RolesConfiguration.Add(CInt(str), Enums.RoleStatus.Remove)
                  End If
               Next

               For Each str As String In ignoreRoles
                  If Not String.IsNullOrEmpty(str) Then
                     _RolesConfiguration.Add(CInt(str), Enums.RoleStatus.Ignore)
                  End If
               Next

            End If

            Return _RolesConfiguration

         End Get
      End Property

      Private ReadOnly Property SearchAllProfiles() As Boolean
         Get
            If Not Settings(Consts.PROFILES_SEARCH_ALL) Is Nothing Then
               Return CBool(Settings(Consts.PROFILES_SEARCH_ALL))
            Else
               Return True
            End If
         End Get
      End Property

      Private ReadOnly Property ShowAllProfiles() As Boolean
         Get
            If Not Settings(Consts.PROFILES_SHOW_ALL) Is Nothing Then
               Return CBool(Settings(Consts.PROFILES_SHOW_ALL))
            Else
               Return True
            End If
         End Get
      End Property

      Private ReadOnly Property ShowProfilesToSearchSettings() As Boolean
         Get
            If Not Settings(Consts.SHOW_PROFILES_TO_SEARCH_SETTINGS) Is Nothing Then
               Return CBool(Settings(Consts.SHOW_PROFILES_TO_SEARCH_SETTINGS))
            Else
               Return True
            End If
         End Get
      End Property

      Private Function CleanColumnName(ByVal columnName As String) As String

         Return DotNetNuke.Common.Utilities.HtmlUtils.StripNonWord(columnName, False).Replace(" ", "")

      End Function

   End Class

End Namespace